package com.test.listview;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.test.listview.module.ExerciseEntity;
import com.test.listview.util.DateTimeUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作业示例
 */
public class TaskDemoActivity extends Activity {
	/** Called when the activity is first created. */
	private final String TAG = "BaseAdapterActivity";

	/** 导航栏底线控件 */
	@InjectView(R.id.nav_bottom)
	ImageView mBottomLine;
	@InjectView(R.id.listview)
	ListView mListView;
	private RecordAdapter mAdapter;

	private LayoutInflater mInflater;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_demo);
		ButterKnife.inject(this);
		findAndSettings();
		new CustomAsyncTask().execute();
	}

	/**
	 * 查找和设置
	 */
	private void findAndSettings() {
		mInflater = LayoutInflater.from(this);
		mAdapter = new RecordAdapter(null);
		mListView.setAdapter(mAdapter);
		setBottomLineView();
	}

	/**
	 * 初始 标题导航栏的底线
	 */
	private void setBottomLineView(){
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mBottomLine.getLayoutParams();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int mScreenWidth = dm.widthPixels;// 获取屏幕分辨率宽度
		params.width = mScreenWidth >> 2;
		params.leftMargin = mScreenWidth >> 3;
		mBottomLine.setLayoutParams(params);
	}

	private List<ExerciseEntity> getData() {
		List<ExerciseEntity> list = new ArrayList<ExerciseEntity>();
		String[] classifyAry = getResources().getStringArray(R.array.exercise_classify);
		String[] nameAry = getResources().getStringArray(R.array.exercise_name);
		String[] testPaperAry = getResources().getStringArray(R.array.exercise_test_paper);

		for (int i = 0; i < 30;i++){
			ExerciseEntity bean = new ExerciseEntity();
			bean.classify = classifyAry[i % 2];
			bean.name = nameAry[i % 5];
			bean.testPaper = testPaperAry[i % 5];
			bean.time = System.currentTimeMillis() - (long)i * 3600 * 24 * 1000;
			list.add(bean);
		}
		return list;
	}

	/**
	 * 记录适配
	 */
	class RecordAdapter extends BaseAdapter {
		private List<ExerciseEntity> appList;

		public RecordAdapter (List<ExerciseEntity> appList) {
			if (appList == null){
				this.appList = new ArrayList<ExerciseEntity>();
			} else {
				this.appList = appList;
			}
		}

		@Override
		public int getCount() {
			return appList.size();
		}

		@Override
		public Object getItem(int position) {
			return appList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.task_demo_item, null);
				holder = new Holder();
				holder.appIconIv = (ImageView) convertView.findViewById(R.id.app_icon);
				holder.appTitleTv = (TextView) convertView.findViewById(R.id.app_title);
				holder.appOpenTimeTv = (TextView) convertView.findViewById(R.id.open_time);
				holder.classifyTv = (TextView) convertView.findViewById(R.id.app_classify);
				holder.lastOpenTv = (TextView) convertView.findViewById(R.id.last_open_tv);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			final ExerciseEntity bean = appList.get(position);
			holder.appIconIv.setBackgroundResource(getIconResId(position));
			holder.classifyTv.setText(bean.classify);
			holder.appTitleTv.setText(bean.name);
			String lastOpen = String.format(getString(R.string.exercise_last_open), bean.testPaper);
			holder.lastOpenTv.setText(lastOpen);
			holder.appOpenTimeTv.setText(DateTimeUtil.format("yyyy-MM-dd", bean.time));

			return convertView;
		}

		/**
		 * 改变数据
		 * @param list
		 */
		public void changeData(List<ExerciseEntity> list){
			appList.clear();
			if (list != null && list.size() > 0){
				appList.addAll(list);
			}
			notifyDataSetChanged();
		}

		/**
		 * 获取资源id
		 * @param position
		 * @return
		 */
		public int getIconResId(int position){
			position = position % 5;
			switch (position){
				case 0:
					return R.drawable.icon1;
				case 1:
					return R.drawable.icon2;
				case 2:
					return R.drawable.icon3;
				case 3:
					return R.drawable.icon4;
				case 4:
					return R.drawable.icon5;
			}
			return 0;
		}



		public class Holder {
			public ImageView appIconIv;
			public TextView appOpenTimeTv;
			public TextView appTitleTv;
			public TextView classifyTv;
			public TextView lastOpenTv;
		}
	}

	/**
	 * 自定义异步线程
	 */
	class CustomAsyncTask extends AsyncTask<Void, Void, List<ExerciseEntity>>{

		@Override
		protected List<ExerciseEntity> doInBackground(Void... params) {
			return getData();
		}

		@Override
		protected void onPostExecute(List<ExerciseEntity> list) {
			mAdapter.changeData(list);
		}
	}
}